package com.hdoit.kylin.web.studentManager.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.studentManager.model.FinalSpeech;
import com.hdoit.kylin.logical.studentManager.service.IFinalSpeechService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.FinalSpeechSearch;
import com.hdoit.kylin.selfcommon.enums.studentManager.FinalSpeechState;

/**
 * @author liuli
 * @email
 * @createtime 2017年07月31日 10:50:26
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/studentManager/finalSpeech/")
public class FinalSpeechController extends BaseController {
	@Override
	public String getRoutePath() {
		return "areas/admin/studentManager/finalSpeech/";
	}

	private IFinalSpeechService finalSpeechService;
	private ITeacherService teacherService;
	private IStudentService studentService;

	public IFinalSpeechService getFinalSpeechService() {
		return finalSpeechService;
	}

	@Resource
	public void setFinalSpeechService(IFinalSpeechService finalSpeechService) {
		this.finalSpeechService = finalSpeechService;
	}

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	private final String indexViewName = "index";
	private final String outputViewName = "output";
	private final String detailedViewName = "finalSpeechDetailed";
	private final String saveViewName = "save";
	private final String deleteViewName = "delete";

	@RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String index(Model model, PagerModel pagerModel,FinalSpeechSearch search, boolean ajaxPost) {
		String groupId=teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		if(StringUtils.isEmpty(groupId)){
			groupId=studentService.queryGroupIdByLoginId(getLoginModel().getLoginId());
		}
		
		pagerModel.setTotalAmount(finalSpeechService.queryCountByGroupId(search,groupId));
		List<FinalSpeech> finalSpeechs = finalSpeechService.queryListByGroupId(search,pagerModel,groupId);
		
		model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
		model.addAttribute("finalSpeechs", finalSpeechs);
		model.addAttribute("finalSpeechSearch", search);
		
		if (ajaxPost) {
			return view(outputViewName);
		}
		return view(indexViewName);
	}

	@RequestMapping(value = detailedViewName, method = { RequestMethod.GET })
	public String finalSpeechDetailed(Model model, String key) {
		FinalSpeech finalSpeech;
		if (!StringUtils.isEmpty(key)) {
			finalSpeech = finalSpeechService.queryModel(key);
		}
		else{
			finalSpeech = new FinalSpeech();
			finalSpeech.setSpeechStatus(FinalSpeechState.waiting);
		}
		model.addAttribute("finalSpeech", finalSpeech);
		return view(detailedViewName);
	}

	@RequestMapping(value = saveViewName, method = { RequestMethod.POST })
	public String save(Model model, FinalSpeech finalSpeech) {
		if (StringUtils.isEmpty(finalSpeech.getId())) {
			finalSpeech.setId(CommonUtils.getKeyId());
			finalSpeech.setFileAddress("");//文件夹地址
			finalSpeech.setModelStatus(ModelStatus.add);
		} else {
			finalSpeech.setFileAddress("");//文件夹地址
			finalSpeech.setModelStatus(ModelStatus.update);
		}
		
		ActionResult actionResult;
		if (finalSpeechService.applyChange(finalSpeech)) {
			actionResult = new ActionResult();
		} else {
			actionResult = new ErrorResult();
		}
		
		model.addAttribute("finalSpeech", finalSpeech);
		model.addAttribute(ManageContainer.RESULTKEY, actionResult);
		
		return view(detailedViewName);
	}

	@RequestMapping(value = deleteViewName, method = { RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public @ResponseBody String delete(String key) {
		if (finalSpeechService.deleteModel(key)) {
			return new JsonActionResult(ManageContainer.SUCCESS).toJson();
		}
		return new JsonActionResult(ManageContainer.ERROR).toJson();
	}
}